package top.glkj.hms.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import top.glkj.hms.entity.User;
import top.glkj.hms.entity.UserDetail;

import java.util.Date;
import java.util.List;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/11 2:34
 */

public interface UserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
    /**
     * 登录查询
     *
     * @param username 用户名
     * @return user
     */
    public User findUserByUsername(String username);

    /**
     * 获取所有正常状态用户
     *
     * @return 正常用户
     */
    @Query("from User where isDel = false and status = 'Enable'")
    public List<User> findUsersByDelIsFalse();

    /**
     * 获取所有被删除的用户
     *
     * @return
     */
    @Query("from User where isDel=true or status='Delete'")
    public List<User> findUsersByDelIsTrue();

    /**
     * 批量更新密码
     *
     * @param ids     用户id集合
     * @param newPass 新密码
     * @return 受影响的行数
     */
    @Query("update User set password = :newPass where id in :ids")
    public int updateAllPassWord(List<Long> ids, String newPass);

    /**
     * 更新密码
     *
     * @param id      用户id
     * @param newPass 新密码
     */
    @Query("update User set password=:newPass where id = :id")
    public void updatePassWord(Long id, String newPass);


    /**
     *
     *  查询少量字段
     * @return 所有用户 无关联查询
     */
    @Query(value = "select id,username,is_del,status,create_user_id,last_login_client," +
            "last_login_ip,lastLoginTime,phone," +
            "update_time,count" +
            " from hms.hms_user",nativeQuery = true)
    public List<User> getAllUser();
    /**
     * 登录查询
     *
     * @param username 用户名
     * @return user
     */
    User getUserByUsername(String username);

    /**
     * 获取用户信息根据用户详情
     * @param userDetail 用户详情
     * @return 用户信息
     */
    User getUserByUserDetail(UserDetail userDetail);

    /**
     * 获取用户信息根据用户详情
     * @param userDetails 用户详情
     * @return 用户信息
     */
    List<User> getAllByUserDetailIn(List<UserDetail> userDetails);

    /**
     * 查询登陆次数总和
     * @return 登陆次数总和
     */
    @Query(value = "select SUM(hms_user.count) from hms_user where lastLoginTime between :startTime and :endTime",nativeQuery = true)
    public long loginCount(Date startTime,Date endTime);


}
